<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="队伍名称：" prop="teamName">
        <el-input
          v-model="queryParams.teamName"
          placeholder="请输入队伍名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="success" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['applet:appletTeam:add']">新增
        </el-button>
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['applet:appletTeam:export']">导出
        </el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="appletTeamList" border>
      <div slot="empty">
        <el-empty description="暂无队伍组织信息！"></el-empty>
      </div>
      <el-table-column label="队伍ID" align="center" prop="teamId" v-if="false"/>
      <el-table-column label="队伍名称" width="300" show-overflow-tooltip align="center" prop="teamName"/>
      <el-table-column label="队伍logo" align="center" prop="teamLogo" width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.teamLogo == null">
            <span style="font-size: 12px;color: gray">暂无信息</span>
          </div>
          <div v-else>
            <image-preview :src="scope.row.teamLogo" :width="50" :height="50"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="累计里程" width="150" align="center" prop="walkSum">
        <template slot-scope="scope">
          {{ scope.row.walkSum }} 里
        </template>
      </el-table-column>
      <el-table-column label="队伍人数" width="150" align="center" prop="peopleSum">
        <template slot-scope="scope">
          {{ scope.row.peopleSum }} 人
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180" align="center" prop="createTime"/>
      <el-table-column label="创建账号" width="120" show-overflow-tooltip align="center" prop="createBy"/>
      <el-table-column label="更新时间" width="180" align="center" prop="updateTime"/>
      <el-table-column label="更新账号" width="120" show-overflow-tooltip align="center" prop="updateBy"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:appletTeam:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:appletTeam:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改appletTeam对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="队伍名称:" prop="teamName">
              <el-input v-model="form.teamName" placeholder="请输入队伍名称"/>
            </el-form-item>
            <el-form-item label="排序" prop="orderBy">
              <el-input-number :min="1" :max="200" v-model="form.orderBy" placeholder="请输入排序"/>
            </el-form-item>
            <el-form-item label="备注:" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>

          </el-col>
          <el-col :span="12">
            <el-form-item label="队伍LOGO:">
              <image-upload v-model="form.teamLogo" :limit="1"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listAppletTeam, getAppletTeam, delAppletTeam, addAppletTeam, updateAppletTeam} from "@/api/applet/appletTeam";

export default {
  name: "AppletTeam",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // appletTeam表格数据
      appletTeamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teamName: null,
        walkSum: null,
        peopleSum: null,
        teamLogo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        teamName: [
          {required: true, message: "队伍名称不能为空", trigger: "blur"}
        ],
        teamLogo: [
          {required: true, message: "队伍LOGO不能为空", trigger: "blur"}
        ],
        orderBy: [
          {required: true, message: "排序不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询appletTeam列表 */
    getList() {
      this.loading = true;
      listAppletTeam(this.queryParams).then(response => {
        this.appletTeamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        teamId: null,
        teamName: null,
        walkSum: null,
        peopleSum: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        teamLogo: null,
        remark: null,
        orderBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.teamId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加队伍组织";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const teamId = row.teamId || this.ids
      getAppletTeam(teamId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改队伍组织";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.teamId != null) {
            updateAppletTeam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAppletTeam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const teamIds = row.teamId || this.ids;
      this.$modal.confirm('是否确认删除 ' + row.teamName + ' 的数据项？').then(function () {
        return delAppletTeam(teamIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('applet/appletTeam/export', {
        ...this.queryParams
      }, `appletTeam_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
